clear all;
clc;

load mnist_train;
load mnist_test;
clear xL;
clear xU;
clear yU;
s = 100;
Y = yL';
clear yL;
Y = [(Y==0) (Y==1) (Y==2) (Y==3) (Y==4) (Y==5) (Y==6) (Y==7) (Y==8) (Y==9)];
Y = Y - (Y==0);
Y = double(Y);
[l,c] = size(Y);
disp('Data loaded!');

load 7nneigsplus;
clear Dvec;
clear IDXvec;

V = V(:,1:s);
P = zeros(l,s,c);
X = zeros(l+s,c);
fval = zeros(c,1);
for i = 1:c
    P = repmat(Y(:,i),1,s).*V(1:l,:);
    cvx_begin
        variable x(s,1)
        variable b
        variable y(l,1) nonnegative
        minimize (sum(y));
        P*x+b*Y(:,i)+y >= ones(l,1);
    cvx_end
    fval(i) = cvx_optval
end

save('res/featest','X','fval');